(function ($) {
    $.fn.nav = function (opts) {
        if (opts != undefined && opts != null) {
            var _url = opts.url;//ajax执行的值。
            var _this = $(this);
            $.ajax({
                url: _url,
                type: "post",
                dataType: "JSON",
                success: function (data) {
                    $.init(opts, _this, data);
                }
            })
        }
    }
    $.init = function (opts, _this, _data) {
        _this.addClass('xy-nav');
        $.initAccordion(opts.children, _data);
        $(_data).each(function () {
            var obj = $(this);
            if (obj != null && obj.length > 0) {
                var _li = $('<li class="xy-nav-item"></li>');
                var _a = $('<a href="javascript:;">' + obj[0].name + '</a>');
                _li.append(_a).appendTo(_this);
                var count = $.countLayer(obj, 1);
                if (count >= 4) {
                    var _dl = $('<dl class="xy-nav-child"></dl>');
                    $(obj[0].children).each(function () {
                        var cobj = $(this);
                        var _dd = $("<dd></dd>");
                        _dd.append("<a href='javascript:;'>" + cobj[0].name + "</a>");
                        _dl.append(_dd);
                        _dd.on('click', function () {
                            $.fn.nav.initAccordion(opts.children, cobj);
                        })
                    });
                    var _span = $('<span class="xy-nav-more"></span>');
                    _a.append(_span);
                    _li.append(_dl);
                    _li.on('click', function () {
                        $(this).addClass('xy-this').siblings().removeClass('xy-this');
                    })
                } else {
                    _li.on('click', function () {
                        $(this).addClass('xy-this').siblings().removeClass('xy-this');
                        $.initAccordion(opts.children, obj);
                    })
                }

                _li.hover(function () {
                    $(this).children().find('span').addClass('xy-nav-mored');
                    $(this).children().filter('.xy-nav-child').addClass('xy-show');
                }, function () {
                    $(this).children().find('span').removeClass('xy-nav-mored');
                    $(this).children().filter('.xy-nav-child').removeClass('xy-show');
                })
            }
        })

    }

    //第一个参数是集合对象，第二个参数是点击的菜单编号
    $.initAccordion = function (_id, obj) {
        //将面板清空
        $($("#" + _id).accordion('panels')).each(function () {
            $("#" + _id).accordion('remove', $(this).panel('options').title);
        })
        //获取点击的按钮的子级
        var children = obj[0].children;
        if (children != null && children.length > 0) {
            $(children).each(function () {
                var _this = $(this)[0];
                //获取其子级，用于判断是否添加展开按钮
                var collapsible = _this.children != null;
                var _ul = "";
                var _title = $('<div><i class="iconfont '+_this.icon+'"></i>' + _this.name + '</div>');
                if (_this.children != null) {
                    _ul = $('<ul class="xy-nav-tree"></ul>');
                    $(_this.children).each(function () {
                        var child = $(this)[0];
                        var _li = $('<li class="xy-nav-item"></li>');
                        var _a = $('<a href="javascript:void(0)"><i class="iconfont '+child.icon+'"></i>' + child.name + '</a>');
                        _li.append(_a);
                        _a.on('click', function () {
                            $('#' + _id + ' .xy-selected').removeClass('xy-selected');
                            $(this).addClass('xy-selected');
                            openTab(child.id, child.name, child.url);
                        })
                        _ul.append(_li);
                    })
                } else {
                    //当没有子级时的点击事件未添加，
                    _title.on('click', function () {
                        openTab(_this.id, _this.name, _this.url);
                    })
                }
                $('#' + _id).accordion("add", {
                    title: _title,
                    collapsible: collapsible,
                    selected: false,
                    content: _ul
                })
            })
        }else{
            var _title = $('<div><i class="iconfont '+obj[0].icon+'"></i>' + obj[0].name + '</div>');
            _title.on('click', function () {
                openTab(obj[0].id, obj[0].name, obj[0].url);
            })
            $('#' + _id).accordion("add", {
                title: _title,
                collapsible: false,
                selected: false,
                content: ""
            })
        }
        $('.accordion-header').on('click', function () {
            if ($(this).parent().children('.accordion-body').children().length == 0) {
                $('#' + _id + ' .xy-selected').removeClass('xy-selected');
                $(this).addClass('xy-selected');
                var allP = $('#' + _id).accordion('panels');
                $(allP).each(function () {
                    $(this).panel('collapse');
                })
            }
        })
    }

    $.countLayer = function(obj, count){
        if (obj[0].children != null) {
            count++;
            $(obj[0].children).each(function () {
                count = $.countLayer($(this), count);
            })
        }
        return count;
    }



})(jQuery)